package com.sczmgk.hotline_pzh.photo.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Thumbnails;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.android.bitmapfun.util.LoadImageThumb;
import com.android.bitmapfun.util.MyImageFetcher;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.photo.entity.PhotoAlbum;

public class PhotoAlbumAdapter extends BaseAdapter {
    private List<PhotoAlbum> aibumList;
    private Context context;
    private List<Integer> lstPosition = new ArrayList<Integer>();
    private List<View> lstView = new ArrayList<View>();

    public PhotoAlbumAdapter(List<PhotoAlbum> list, Context context) {
        this.aibumList = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return aibumList.size();
    }

    @Override
    public Object getItem(int position) {
        return aibumList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	ViewHolder viewHolder = null;
    	PhotoAlbum photoAlbum = aibumList.get(position);
        if (convertView==null||convertView.getTag()==null) {

        	viewHolder = new ViewHolder();
        	
            convertView = LayoutInflater.from(context).inflate( R.layout.photoalbum_item, null);
                   
            viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.photoalbum_item_image);
                    
            viewHolder.mTVMessage = (TextView) convertView.findViewById(R.id.photoalbum_item_name);
                    
            viewHolder.mImageView.setScaleType(ScaleType.CENTER_CROP);
            
            convertView.setTag(viewHolder);
        } else {
        	viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.mTVMessage.setText(photoAlbum.getName() + " ( " + photoAlbum.getCount() + " )");
        
        MyImageFetcher.getInstance().loadImage(new LoadImageThumb(context, Thumbnails.MINI_KIND, photoAlbum.getBitmap(), 200, 200), 
        		viewHolder.mImageView);
        return convertView;
    }
    
    private class ViewHolder {
    	private ImageView mImageView;
    	private TextView mTVMessage;
    	
    }

    private class asynLoadBitmap extends AsyncTask<Object, Object, Void> {

        @Override
        protected Void doInBackground(Object... params) {
            ImageView view = (ImageView) params[0];
            Bitmap bitmap = Thumbnails.getThumbnail(
                    context.getContentResolver(), (Integer) params[1],
                    Thumbnails.MICRO_KIND, null);
            publishProgress(new Object[] { view, bitmap });
            return null;
        }

        protected void onProgressUpdate(Object... progress) {
            ImageView view = (ImageView) progress[0];
            if (null != (Bitmap) progress[1]) {
                view.setImageBitmap((Bitmap) progress[1]);
            } else {
                view.setImageResource(R.drawable.default_center);
            }
        }

    }

}

